﻿using System;
using System.IO;
using System.Windows.Forms;
using GMap.NET;
using GMap.NET.WindowsForms;
using LetsDoIt.BO;
using LetsDoIt.Common;
using LetsDoIt.Controls;
using LetsDoIt.Interfaces;

namespace LetsDoIt.Forms
{
	public partial class MapCaseForm : UserControl, INavigationForm
	{
		public string ID { get; set; }
		GMapOverlay objects;


		#region ctor

		private MapCaseForm()
		{
			InitializeComponent();
		}

		public MapCaseForm(MainForm hostForm, string identity)
		{
			HostForm = hostForm;
			Identity = identity;
			InitializeComponent();
            
            GMaps.Instance.Mode = AccessMode.ServerAndCache;
			caseMap.Manager.Mode = AccessMode.ServerAndCache;
			caseMap.MapType = MapType.GoogleSatellite;
			caseMap.MaxZoom = 17;
			caseMap.MinZoom = 5;
			caseMap.Zoom = 15;

			objects = new GMapOverlay(caseMap, "objects");
			caseMap.Overlays.Add(objects);
		}

		#endregion ctor


		#region Implementation of INavigationForm

		public MainForm HostForm { get; set; }
		public string Identity { get; set; }
        public bool IsActive { get; set; }

		public void NavigationEnter(object sender)
		{
			//nothing
			ViewCasesForm frmViewCases = HostForm.GetFormByIdentity(NavigationForms.FORM_VIEW_CASES) as ViewCasesForm;
			if (frmViewCases != null)
			{
				caseMap.Visible = true;
				CaseInfo ci = frmViewCases.SelectedItem.Case;
				PointLatLng position = new PointLatLng(ci.Latitude, ci.Longitude);
                foreach (WasteMapPointMarker marker in objects.Markers)
			    {
			        marker.Dispose();
			    }
				objects.Markers.Clear();
			    string pictureFile = ci.Pictures.Count > 0 ? Path.Combine(WastesLocalFactory.CasesStorePath(),ci.Pictures[0]) : string.Empty;
                GMapMarker m = new WasteMapPointMarker(position, ci.Description, pictureFile, caseMap.Width);// GMapMarkerGoogleGreen(position);
				objects.Markers.Add(m);
				caseMap.CurrentPosition = position;
			}
			else
			{
				caseMap.Visible = false;
			}
		}

		public void NavigationLeave()
		{
			//nothing
		}

		#endregion


		#region events

		private void btnBack_Click(object sender, EventArgs e)
		{
			HostForm.NavigateBack(this, string.Empty);
		}

        private void picZoomIn_Click(object sender, EventArgs e)
        {
            caseMap.Zoom = Math.Min(caseMap.Zoom + 1, caseMap.MaxZoom);
        }

        private void picZoomOut_Click(object sender, EventArgs e)
        {
            caseMap.Zoom = Math.Max(caseMap.Zoom - 1, caseMap.MinZoom);
        }

		#endregion events

	}
}
